package com.competition.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.competition.dao.EquipmentAuditDao;
import com.competition.entity.EquipmentAudit;
import com.competition.entity.EquipmentLeasingOrder;
import com.competition.mapper.EquipmentAuditMapper;
import com.competition.resp.ResponseResult;
import com.competition.service.EquipmentAuditService;
import com.competition.service.EquipmentLeasingOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/equipmentAudit")
public class EquipmentAuditController {

    @Autowired
    EquipmentAuditService equipmentAuditService;

    @Autowired
    EquipmentLeasingOrderService equipmentLeasingOrderService;

    @Autowired
    private EquipmentAuditMapper equipmentAuditMapper;

    @GetMapping("/queryEquipmentAuditList")
    public ResponseResult<Object> queryEquipmentAuditList(@RequestParam HashMap<String,Object> params){
        if(params.get("page")==null||params.get("pageSize")==null){
            return new ResponseResult<Object>(400,"参数错误",null);
        }
        int page = Integer.parseInt(params.get("page").toString());
        int pageSize = Integer.parseInt(params.get("pageSize").toString());
        HashMap<String, Object> reqMap = params;
        reqMap.put("limit",pageSize);
        reqMap.put("offset",(page - 1) * pageSize);
        List<EquipmentAudit> equipmentAuditList =   equipmentAuditService.queryEquipmentAuditList(reqMap);
        long count = equipmentAuditService.count();
        HashMap<String,Object> resMap = new HashMap<>();
        resMap.put("equipmentAuditList",equipmentAuditList);
        resMap.put("total",count);
        return   new ResponseResult<Object>(200,"获取预约列表成功",resMap);
    }


    @PutMapping("/modifyEquipmentAuditStatus")
    @Transactional
    public ResponseResult<Object> updateEquipmentAuditStatus(@RequestBody HashMap<String,Object> params){
        if(params.get("auditId")==null || params.get("auditStatus")==null){
            return new ResponseResult<Object>(400,"参数错误",null);
        }
        UpdateWrapper<EquipmentAudit> objectUpdateWrapper = new UpdateWrapper<>();
        objectUpdateWrapper.eq("audit_id",params.get("auditId"));
        objectUpdateWrapper.set("audit_status",params.get("auditStatus"));
        equipmentAuditService.update(objectUpdateWrapper);
        if(params.get("auditStatus").equals("审核通过")){
            QueryWrapper<EquipmentAudit> objectQueryWrapper = new QueryWrapper<>();
            objectQueryWrapper.eq("audit_id",params.get("auditId"));
            EquipmentAudit equipmentAudit = equipmentAuditService.getOne(objectQueryWrapper);
            EquipmentLeasingOrder equipmentLeasingOrder = new EquipmentLeasingOrder();
            UUID uuid =  UUID.randomUUID();
            equipmentLeasingOrder.setEquipmentOrderId(uuid.toString());
            equipmentLeasingOrder.setEquipmentId(equipmentAudit.getEquipmentId());
            equipmentLeasingOrder.setEquipmentOrderCount(equipmentAudit.getAuditCount());
            equipmentLeasingOrder.setEquipmentOrderStatus("未归还");
            equipmentLeasingOrder.setUserId(equipmentAudit.getUserId());
            equipmentLeasingOrderService.save(equipmentLeasingOrder);
            return new ResponseResult<Object>(200, "审批成功", null);
        }else{
            return new ResponseResult<Object>(200, "拒绝成功", null);
        }
    }
    @GetMapping("/getEquipmentAudit")
    public ResponseResult getEquipmentAudit(@RequestParam("openId")String openId,@RequestParam("through")String through,@RequestParam("current")int current){
        if (openId == null || through == null || current<0){
            return new ResponseResult(400,"参数错误",null);
        }
        try {
            List<EquipmentAuditDao> equipmentAudits = equipmentAuditMapper.selectAllByUserIdAndAuditStatusAndAuditStatus(openId, through,current);
            return new ResponseResult(200,"获取成功",equipmentAudits);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(300,"获取异常",null);
        }
    }
    @GetMapping("/getEquipmentAuditByName")//模糊查询
    public ResponseResult getEquipmentAuditByName(@RequestParam("name")String name,@RequestParam("openId")String openId,@RequestParam("through")String through,@RequestParam("current")int current){
        if (openId == null || through == null || current<0 || name ==null){
            return new ResponseResult(400,"参数错误",null);
        }
        try {
            List<EquipmentAuditDao> equipmentAuditDaos = equipmentAuditMapper.selectByMoHu(openId, through, current, name);
            return new ResponseResult(200,"获取成功",equipmentAuditDaos);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(300,"获取异常",null);
        }
    }

}
